﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Clinica_Frba.RegistrarAgenda
{
    public partial class agenda : Form
    {
        public agenda()
        {
            InitializeComponent();
        }
        int idp;
        public agenda(int id)
        {
            InitializeComponent();
            idp = id;
        }

        private void comboBox11_SelectedIndexChanged(object sender, EventArgs e)
        {

        }



        private void button1_Click(object sender, EventArgs e)
        {
            int contador;
            string fecha,fecha2;
            DateTime date,date2;
            fecha = textBox1.Text; 
            if (DateTime.TryParse(fecha, out date)) { } else { return; }
            fecha2 = textBox2.Text;
            if (DateTime.TryParse(fecha2, out date2)) { } else { return; }
            date = DateTime.Parse(fecha);
            date2 = DateTime.Parse(fecha2);
            
            int diff = ((TimeSpan)(date2 - date)).Days;
            if (diff > 120 | diff<0 ) {
                if (diff > 120)
                {
                    MessageBox.Show("No se puede generar una agenda para un periodo superior a 120 dias", "Agenda profesional");
                }

                if (diff < 0)
                {
                    MessageBox.Show("La fecha inicial es posterior a la final", "Agenda profesional");
                }
                
                return; }
            SqlConnection myConnection = new SqlConnection(publico.ConnectionString);
            myConnection.Open();
            SqlDataReader dr;
            SqlCommand verificarfecha = new SqlCommand("SELECT VENCIMIENTO FROM DUNDER_MIFFLIN.AGENDA_PROF WHERE CONVERT(datetime,VENCIMIENTO,103)>CONVERT(datetime,'" + textBox1.Text + "',103) AND ID_PROF="+idp, myConnection);
            dr = verificarfecha.ExecuteReader();
            if (dr.HasRows) { MessageBox.Show("El profesional ya tiene una agenda con la fecha especificada", "Agenda profesional"); return; }
            dr.Close();
            SqlCommand initran = new SqlCommand("BEGIN TRAN AP",myConnection);
            initran.ExecuteNonQuery();
            string minutos, minutos2,hora,hora2;
            int idlast,h,h2;
            contador = 0;
            if (checkBox1.Checked)
            {
                hora = maskedTextBox1.Text.Substring(0, 2);
                hora2 = maskedTextBox2.Text.Substring(0, 2);
                h = Convert.ToInt32(hora);
                h2 = Convert.ToInt32(hora2);
                contador = contador + h2 - h;
            }
            if (checkBox2.Checked)
            {
                hora = maskedTextBox3.Text.Substring(0, 2);
                hora2 = maskedTextBox4.Text.Substring(0, 2);
                h = Convert.ToInt32(hora);
                h2 = Convert.ToInt32(hora2);
                contador = contador + h2 - h;
            }
            if (checkBox3.Checked)
            {
                hora = maskedTextBox5.Text.Substring(0, 2);
                hora2 = maskedTextBox6.Text.Substring(0, 2);
                h = Convert.ToInt32(hora);
                h2 = Convert.ToInt32(hora2);
                contador = contador + h2 - h;
            }
            if (checkBox4.Checked)
            {
                hora = maskedTextBox7.Text.Substring(0, 2);
                hora2 = maskedTextBox8.Text.Substring(0, 2);
                h = Convert.ToInt32(hora);
                h2 = Convert.ToInt32(hora2);
                contador = contador + h2 - h;
            }
            if (checkBox5.Checked)
            {
                hora = maskedTextBox9.Text.Substring(0, 2);
                hora2 = maskedTextBox10.Text.Substring(0, 2);
                h = Convert.ToInt32(hora);
                h2 = Convert.ToInt32(hora2);
                contador = contador + h2 - h;
            }
            if (checkBox6.Checked)
            {
                hora = maskedTextBox11.Text.Substring(0, 2);
                hora2 = maskedTextBox12.Text.Substring(0, 2);
                h = Convert.ToInt32(hora);
                h2 = Convert.ToInt32(hora2);
                contador = contador + h2 - h;
            }
            if (contador > 48) { MessageBox.Show("No puede tener mas de 48 horas por semana", "Registro de agenda profesional"); return; }
            SqlCommand ii, getid, insert,saveid;
            try
            {
                ii = new SqlCommand("SET IDENTITY_INSERT DUNDER_MIFFLIN.AGENDA_PROF ON", myConnection);
                ii.ExecuteNonQuery();
                if (checkBox1.Checked)
                {
                    hora = maskedTextBox1.Text.Substring(0, 2);
                    hora2 = maskedTextBox2.Text.Substring(0, 2);
                    h=Convert.ToInt32(hora);
                    h2=Convert.ToInt32(hora2);
                    if (h < 7 | h > 20 | h2 < 7 | h2 > 20 | h > h2) { throw new Exception("Debe ingresar horas validas"); }
                    minutos = maskedTextBox1.Text.Substring(maskedTextBox1.Text.Length - 2);
                    minutos2 = maskedTextBox2.Text.Substring(maskedTextBox2.Text.Length - 2);
                    if (minutos == "00" | minutos == "30" | minutos2 == "00" | minutos2 == "30") { } else { throw new Exception("Debe ingresar turnos de cada media hora"); }

                    getid = new SqlCommand("SELECT ULTIMOID FROM DUNDER_MIFFLIN.PROVIDERID WHERE TABLA='AGENDA'", myConnection);
                    idlast = Convert.ToInt32(getid.ExecuteScalar());
                    idlast++;
                    insert = new SqlCommand("INSERT INTO DUNDER_MIFFLIN.AGENDA_PROF(ID_AGENDA_P,Dia,HORA_INICIO,HORA_FIN,VENCIMIENTO,ID_PROF,FECHA_INICIO) VALUES(" + idlast + ",'" + checkBox1.Text + "',CONVERT(time,'" + maskedTextBox1.Text + ":00',14),CONVERT(time,'" + maskedTextBox2.Text + ":00',14),CONVERT(datetime,'" + textBox2.Text + "',103)," + idp + ",CONVERT(datetime,'" + textBox1.Text + "',103))", myConnection);
                    
                    insert.ExecuteNonQuery();
                    saveid = new SqlCommand("UPDATE DUNDER_MIFFLIN.PROVIDERID SET ULTIMOID=" + idlast + " WHERE TABLA='AGENDA'", myConnection);
                    saveid.ExecuteNonQuery();
                    ii.Dispose();
                    getid.Dispose();
                    insert.Dispose();
                    saveid.Dispose();
                }
                if (checkBox2.Checked)
                {
                    hora = maskedTextBox3.Text.Substring(0, 2);
                    hora2 = maskedTextBox4.Text.Substring(0, 2);
                    h = Convert.ToInt32(hora);
                    h2 = Convert.ToInt32(hora2);
                    if (h < 7 | h > 20 | h2 < 7 | h2 > 20 | h > h2) { throw new Exception("Debe ingresar horas validas"); }
                    minutos = maskedTextBox3.Text.Substring(maskedTextBox1.Text.Length - 2);
                    minutos2 = maskedTextBox4.Text.Substring(maskedTextBox2.Text.Length - 2);
                    if (minutos == "00" | minutos == "30" | minutos2 == "00" | minutos2 == "30") { } else { throw new Exception("Debe ingresar turnos de cada media hora"); }
                    getid = new SqlCommand("SELECT ULTIMOID FROM DUNDER_MIFFLIN.PROVIDERID WHERE TABLA='AGENDA'", myConnection);
                    idlast = Convert.ToInt32(getid.ExecuteScalar());
                    idlast++;
                    insert = new SqlCommand("INSERT INTO DUNDER_MIFFLIN.AGENDA_PROF(ID_AGENDA_P,Dia,HORA_INICIO,HORA_FIN,VENCIMIENTO,ID_PROF,FECHA_INICIO) VALUES(" + idlast + ",'" + checkBox2.Text + "',CONVERT(time,'" + maskedTextBox3.Text + ":00',14),CONVERT(time,'" + maskedTextBox4.Text + ":00',14),CONVERT(datetime,'" + textBox2.Text + "',103)," + idp + ",CONVERT(datetime,'" + textBox1.Text + "',103))", myConnection);
                    insert.ExecuteNonQuery();
                    saveid = new SqlCommand("UPDATE DUNDER_MIFFLIN.PROVIDERID SET ULTIMOID=" + idlast + " WHERE TABLA='AGENDA'", myConnection);
                    saveid.ExecuteNonQuery();
                    ii.Dispose();
                    getid.Dispose();
                    insert.Dispose();
                    saveid.Dispose();
                }
                if (checkBox3.Checked)
                {
                    hora = maskedTextBox5.Text.Substring(0, 2);
                    hora2 = maskedTextBox6.Text.Substring(0, 2);
                    h = Convert.ToInt32(hora);
                    h2 = Convert.ToInt32(hora2);
                    if (h < 7 | h > 20 | h2 < 7 | h2 > 20 | h > h2) { throw new Exception("Debe ingresar horas validas"); }
                    minutos = maskedTextBox5.Text.Substring(maskedTextBox1.Text.Length - 2);
                    minutos2 = maskedTextBox6.Text.Substring(maskedTextBox2.Text.Length - 2);
                    if (minutos == "00" | minutos == "30" | minutos2 == "00" | minutos2 == "30") { } else { throw new Exception("Debe ingresar turnos de cada media hora"); }
                    getid = new SqlCommand("SELECT ULTIMOID FROM DUNDER_MIFFLIN.PROVIDERID WHERE TABLA='AGENDA'", myConnection);
                    idlast = Convert.ToInt32(getid.ExecuteScalar());
                    idlast++;
                    insert = new SqlCommand("INSERT INTO DUNDER_MIFFLIN.AGENDA_PROF(ID_AGENDA_P,Dia,HORA_INICIO,HORA_FIN,VENCIMIENTO,ID_PROF,FECHA_INICIO) VALUES(" + idlast + ",'" + checkBox3.Text + "',CONVERT(time,'" + maskedTextBox5.Text + ":00',14),CONVERT(time,'" + maskedTextBox6.Text + ":00',14),CONVERT(datetime,'" + textBox2.Text + "',103)," + idp + ",CONVERT(datetime,'" + textBox1.Text + "',103))", myConnection);
                    insert.ExecuteNonQuery();
                    saveid = new SqlCommand("UPDATE DUNDER_MIFFLIN.PROVIDERID SET ULTIMOID=" + idlast + " WHERE TABLA='AGENDA'", myConnection);
                    saveid.ExecuteNonQuery();
                    ii.Dispose();
                    getid.Dispose();
                    insert.Dispose();
                    saveid.Dispose();
                }
                if (checkBox4.Checked)
                {
                    hora = maskedTextBox7.Text.Substring(0, 2);
                    hora2 = maskedTextBox8.Text.Substring(0, 2);
                    h = Convert.ToInt32(hora);
                    h2 = Convert.ToInt32(hora2);
                    if (h < 7 | h > 20 | h2 < 7 | h2 > 20 | h > h2) { throw new Exception("Debe ingresar horas validas"); }
                    minutos = maskedTextBox7.Text.Substring(maskedTextBox1.Text.Length - 2);
                    minutos2 = maskedTextBox8.Text.Substring(maskedTextBox2.Text.Length - 2);
                    if (minutos == "00" | minutos == "30" | minutos2 == "00" | minutos2 == "30") { } else { throw new Exception("Debe ingresar turnos de cada media hora"); }
                    getid = new SqlCommand("SELECT ULTIMOID FROM DUNDER_MIFFLIN.PROVIDERID WHERE TABLA='AGENDA'", myConnection);
                    idlast = Convert.ToInt32(getid.ExecuteScalar());
                    idlast++;
                    insert = new SqlCommand("INSERT INTO DUNDER_MIFFLIN.AGENDA_PROF(ID_AGENDA_P,Dia,HORA_INICIO,HORA_FIN,VENCIMIENTO,ID_PROF,FECHA_INICIO) VALUES(" + idlast + ",'" + checkBox4.Text + "',CONVERT(time,'" + maskedTextBox7.Text + ":00',14),CONVERT(time,'" + maskedTextBox8.Text + ":00',14),CONVERT(datetime,'" + textBox2.Text + "',103)," + idp + ",CONVERT(datetime,'" + textBox1.Text + "',103))", myConnection);
                    insert.ExecuteNonQuery();
                    saveid = new SqlCommand("UPDATE DUNDER_MIFFLIN.PROVIDERID SET ULTIMOID=" + idlast + " WHERE TABLA='AGENDA'", myConnection);
                    saveid.ExecuteNonQuery();
                    ii.Dispose();
                    getid.Dispose();
                    insert.Dispose();
                    saveid.Dispose();
                }
                if (checkBox5.Checked)
                {
                    hora = maskedTextBox9.Text.Substring(0, 2);
                    hora2 = maskedTextBox10.Text.Substring(0, 2);
                    h = Convert.ToInt32(hora);
                    h2 = Convert.ToInt32(hora2);
                    if (h < 7 | h > 20 | h2 < 7 | h2 > 20 | h > h2) { throw new Exception("Debe ingresar horas validas"); }
                    minutos = maskedTextBox9.Text.Substring(maskedTextBox1.Text.Length - 2);
                    minutos2 = maskedTextBox10.Text.Substring(maskedTextBox2.Text.Length - 2);
                    if (minutos == "00" | minutos == "30" | minutos2 == "00" | minutos2 == "30") { } else { throw new Exception("Debe ingresar turnos de cada media hora"); }
                    getid = new SqlCommand("SELECT ULTIMOID FROM DUNDER_MIFFLIN.PROVIDERID WHERE TABLA='AGENDA'", myConnection);
                    idlast = Convert.ToInt32(getid.ExecuteScalar());
                    idlast++;
                    insert = new SqlCommand("INSERT INTO DUNDER_MIFFLIN.AGENDA_PROF(ID_AGENDA_P,Dia,HORA_INICIO,HORA_FIN,VENCIMIENTO,ID_PROF,FECHA_INICIO) VALUES(" + idlast + ",'" + checkBox5.Text + "',CONVERT(time,'" + maskedTextBox9.Text + ":00',14),CONVERT(time,'" + maskedTextBox10.Text + ":00',14),CONVERT(datetime,'" + textBox2.Text + "',103)," + idp + ",CONVERT(datetime,'" + textBox1.Text + "',103))", myConnection);
                    insert.ExecuteNonQuery();
                    saveid = new SqlCommand("UPDATE DUNDER_MIFFLIN.PROVIDERID SET ULTIMOID=" + idlast + " WHERE TABLA='AGENDA'", myConnection);
                    saveid.ExecuteNonQuery();
                    ii.Dispose();
                    getid.Dispose();
                    insert.Dispose();
                    saveid.Dispose();
                }
                if (checkBox6.Checked)
                {
                    hora = maskedTextBox11.Text.Substring(0, 2);
                    hora2 = maskedTextBox12.Text.Substring(0, 2);
                    h = Convert.ToInt32(hora);
                    h2 = Convert.ToInt32(hora2);
                    if (h < 8 | h > 15 | h2 < 8 | h2 > 15 | h > h2) { throw new Exception("Debe ingresar horas validas"); }
                    minutos = maskedTextBox11.Text.Substring(maskedTextBox11.Text.Length - 2);
                    minutos2 = maskedTextBox12.Text.Substring(maskedTextBox12.Text.Length - 2);
                    if (minutos == "00" | minutos == "30" | minutos2 == "00" | minutos2 == "30") { } else { throw new Exception("Debe ingresar turnos de cada media hora"); }
                    getid = new SqlCommand("SELECT ULTIMOID FROM DUNDER_MIFFLIN.PROVIDERID WHERE TABLA='AGENDA'", myConnection);
                    idlast = Convert.ToInt32(getid.ExecuteScalar());
                    idlast++;
                    insert = new SqlCommand("INSERT INTO DUNDER_MIFFLIN.AGENDA_PROF(ID_AGENDA_P,Dia,HORA_INICIO,HORA_FIN,VENCIMIENTO,ID_PROF,FECHA_INICIO) VALUES(" + idlast + ",'" + checkBox6.Text + "',CONVERT(time,'" + maskedTextBox11.Text + ":00',14),CONVERT(time,'" + maskedTextBox12.Text + ":00',14),CONVERT(datetime,'" + textBox2.Text + "',103)," + idp + ",CONVERT(datetime,'" + textBox1.Text + "',103))", myConnection);
                    insert.ExecuteNonQuery();
                    saveid = new SqlCommand("UPDATE DUNDER_MIFFLIN.PROVIDERID SET ULTIMOID=" + idlast + " WHERE TABLA='AGENDA'", myConnection);
                    saveid.ExecuteNonQuery();
                    ii.Dispose();
                    getid.Dispose();
                    insert.Dispose();
                    saveid.Dispose();
                }
                SqlCommand c = new SqlCommand("COMMIT TRAN AP", myConnection);
                c.ExecuteNonQuery();
                MessageBox.Show("Se ha guardado correctamente", "Agenda profesional");
          
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message, "Agenda profesional");
                SqlCommand rb = new SqlCommand("ROLLBACK TRAN AP", myConnection);
                rb.ExecuteNonQuery();
            }

        }

        private void maskedTextBox10_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {

        }

        private void agenda_Load(object sender, EventArgs e)
        {

        }
    }
}
